package com.dries.portal.module.circle.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.dries.common.api.CommonResult;
import com.dries.portal.module.circle.entity.CircleLikes;
import com.dries.portal.module.circle.service.ICircleLikesService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * 圈子点赞表
 *
 * @author dries
 * @email samphsanie@gmail.com
 * @date 2020-08-26 17:40:34
 */
@RestController
@Api(tags = "火聚圈点赞API", value = "CircleLikesController")
@RequestMapping("circle/likes")
public class CircleLikesController {

    @Resource
    private ICircleLikesService circleLikesService;

    /**
     * 保存
     */
    @ApiOperation("点赞")
    @PostMapping("/save")
    public CommonResult save(@RequestBody CircleLikes circleLikes){
        CircleLikes like = circleLikesService.getOne(new QueryWrapper<CircleLikes>().lambda()
                .eq(CircleLikes::getCircleId, circleLikes.getCircleId())
                .eq(CircleLikes::getUserId, circleLikes.getUserId())
        );

        if (like == null) {
            circleLikesService.save(circleLikes);
        }

        return CommonResult.success();
    }

    /**
     * 删除
     */
    @ApiOperation("取消点赞")
    @DeleteMapping("/delete/{userId}/{circleId}")
    public CommonResult delete(@PathVariable("userId") String userId,@PathVariable Integer circleId) {

		// circleLikesService.removeByIds(Arrays.asList(ids));

		circleLikesService.remove(new QueryWrapper<CircleLikes>().lambda()
                .eq(CircleLikes::getUserId, userId)
                .in(CircleLikes::getCircleId, circleId)
        );

        return CommonResult.success();
    }

}
